Use new cached project count column (part 2 of 2)#805
Conversation
Test coverage91.08% line coverage reported by SimpleCov. |
9def7db to
6ff71db
Compare
ed1ecb5 to
a503b60
Compare
In [1] we started caching the value of submitted projects. Now we can use that value to simplify the code and reduce N+1 queries. [1] #804 Note that I've had to split the loading of remixes for teachers and students as teachers no longer need remixes (which causes bullet to complain) Co-authored-by: Copilot <copilot@github.com>
a503b60 to
7c74295
Compare
There was a problem hiding this comment.
Pull request overview
This PR (part 2 of 2) leverages the submitted_projects_count counter cache column introduced in #804 to remove N+1 query patterns when listing lessons and classes. The custom submitted_count methods on Lesson and SchoolClass (which joined through projects, remixes, school_projects, and transitions) are replaced with reads of the cached column, and controller includes are simplified accordingly.
Changes:
- Replaces
Lesson#submitted_countusage with the cachedsubmitted_projects_countcolumn, and computesSchoolClass#submitted_projects_countas the sum over its (preloaded) lessons. - Simplifies eager-loading in
LessonsController#indexandSchoolClassesController#accessible_school_classes(no more loading the deepproject → remixes → school_project → transitionschain for teacher views). - Updates jbuilder views and specs to use the new cached value (specs now set
submitted_projects_countdirectly via the factory).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| app/models/lesson.rb | Removes the old #submitted_count method that counted submitted remixes in Ruby. |
| app/models/school_class.rb | Replaces the SQL-join-based #submitted_count with #submitted_projects_count summing the cached column over lessons. |
| app/controllers/api/lessons_controller.rb | Drops deep includes(project: :remixes) for the teacher branch; keeps it only for the student branch that still needs remixes. |
| app/controllers/api/school_classes_controller.rb | Replaces the deep includes chain with a simple includes(:lessons) so the sum can use preloaded lessons. |
| app/views/api/lessons/teacher_index.json.jbuilder | Reads submitted_projects_count instead of submitted_count. |
| app/views/api/school_classes/teacher_index.json.jbuilder | Reads submitted_projects_count instead of submitted_count. |
| spec/models/lesson_spec.rb | Removes the now-obsolete #submitted_count examples (covered by counter-cache specs). |
| spec/models/school_class_spec.rb | Rewrites the spec to set submitted_projects_count on lessons via the factory and asserts the summed value. |
| spec/features/lesson/listing_lessons_spec.rb | Simplifies the listing spec by setting submitted_projects_count directly on the lesson. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
MUST BE DEPLOYED AFTER #804
Status
Related to: https://github.com/RaspberryPiFoundation/digital-editor-issues/issues/1313
Follows on from: #804
What's changed?
In part 1 we started caching the value of submitted projects. Now we can use that value to simplify the code and reduce N+1 queries.